﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using Weixin.BLL;
using Weixin.Utils;

namespace Weixin.Web
{
    public partial class Ask : System.Web.UI.Page
    {
        int _Page;
        /// <summary>
        /// 1热度排序2时间倒序
        /// </summary>
        int _Sort;
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["page"], out _Page);
            int.TryParse(Request.QueryString["sortId"], out _Sort);

            if (_Page > 0)
            {
                GetData();
            }
        }

        void GetData()
        {
            int pageCount = 0;
            int recordCount = 0;
            Common common = new Common();
            string strWhere = string.Empty;
            strWhere = " and flag=1 and isHot=1";
            string strSort = string.Empty;
            if (_Sort == 1)
            {
                strSort = "hits desc";
            }
            if (_Sort == 2)
            {
                strSort = "Id desc";
            }
            DataSet ds = common.GetTablePageList("Ask", "Id", strSort, "*", strWhere, _Page, 10, out recordCount);
            pageCount = recordCount / 10;
            if (recordCount % 10 > 0)
            {
                pageCount = recordCount / 10 + 1;
            }
            else
            {
                pageCount = recordCount / 10;
            }
            StringBuilder strBuilder = new StringBuilder();
            StringBuilder strBuilder2 = new StringBuilder();
            strBuilder.Append("{\"pageCount\":\"" + pageCount + "\",\"items\":");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                strBuilder.Append("[");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (strBuilder2.ToString() != "")
                    {
                        strBuilder2.Append(",");
                    }
                    strBuilder2.Append("{");
                    strBuilder2.Append("\"id\":\"" + dr["id"] + "\",");
                    strBuilder2.Append("\"sortId\":\"" + _Sort + "\",");
                    strBuilder2.Append("\"title\":\"" + StringOperate.CutStr(dr["title"].ToString(),36) + "\"");
                    strBuilder2.Append("}");
                }
                strBuilder.Append(strBuilder2.ToString());
                strBuilder.Append("]");
            }
            strBuilder.Append("}");

            Response.Write(strBuilder.ToString());
            Response.End();
        }
    }
}